<?php

namespace app\controllers;

use Yii;
use app\models\Payment;
use app\models\PaymentSearch;
use app\models\AccPayAddress;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * PaymentController implements the CRUD actions for Payment model.
 */
class PaymentController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Payment models.
     * @return mixed
     */
    public function actionIndex($acc_id=null)
    {
        $searchModel = new PaymentSearch();
		if($acc_id==null)
		{
			$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
			return $this->render('index', [
				'searchModel' => $searchModel,
				'dataProvider' => $dataProvider,
			]);
		}
		else
		{
			$dataProvider = $searchModel->searchByUser(Yii::$app->request->getQueryParams());
			return $this->render('index_user', [
				'acc_id' => $acc_id,
				'searchModel' => $searchModel,
				'dataProvider' => $dataProvider,
			]);
		}
    }

    /**
     * Displays a single Payment model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Payment model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Payment();

        if ($model->load(Yii::$app->request->post())) {			
			$model->acc_pay_add_id = Yii::$app->user->identity->accPayAddresses[0]->acc_pay_address_id;
			$model->save();
			//gửi mail cho admin
			Yii::$app->mail->compose()
				 ->setFrom('hotro@thongtinmientay.vn')
				 ->setTo(UserController::GetAdminEmail())
				 ->setSubject('[CAPTCHA TTMT] Thông báo')
				 ->setHtmlBody('<b>Chào quản trị viên,</b><br/>
				 Có 1 thành viên yêu cầu thanh toán mới.<br/>
				 Vui lòng kiểm tra và thanh toán cho thành viên <a href='.
				 Yii::$app->UrlManager->createAbsoluteUrl('/').'>tại đây.</a><br/>
				 Trân trọng!')
				 ->send();
            return $this->redirect(['view', 'id' => $model->payment_id]);
        } else {
            return $this->render('create', [
                'model' => $model,
                'arrayPayAddPro' => AccPayAddressController::getPayAddPro(),
            ]);
        }
    }

    /**
     * Updates an existing Payment model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
		$model2 = new Payment();
        if ($model2->load(Yii::$app->request->post())) {
            $model->amount = $model2->amount;
            $model->status = 1;
			$model->save();
            return $this->redirect(['view', 'id' => $model->payment_id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Payment model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Payment model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Payment the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Payment::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
